{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "KEY = 'c25fe3dd21454182b051ac79e291af1d' \n",
    "BASE_URL = 'https://api-cq.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY),  \n",
    "}\n",
    "\n",
    "img_url = 'http://img1.imgtn.bdimg.com/it/u=3567202712,531457823&fm=26&gp=0.jpg'\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error': {'code': 'InvalidURL',\n",
       "  'message': 'Invalid image URL or error downloading from target server. Remote server error returned: \"Response status code does not indicate success: 403 (Forbidden).\"'}}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "r.json()\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>e8686ff7-d0c6-4ba3-96af-243f72b70292</td>\n",
       "      <td>118</td>\n",
       "      <td>144</td>\n",
       "      <td>88</td>\n",
       "      <td>88</td>\n",
       "      <td>male</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.813</td>\n",
       "      <td>0.184</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>36f11517-9ca2-424f-ac5d-026569b63806</td>\n",
       "      <td>117</td>\n",
       "      <td>376</td>\n",
       "      <td>64</td>\n",
       "      <td>64</td>\n",
       "      <td>female</td>\n",
       "      <td>20.0</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.456</td>\n",
       "      <td>0.542</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>e19d516c-c49d-40aa-aaed-2f36bc51ccbe</td>\n",
       "      <td>41</td>\n",
       "      <td>676</td>\n",
       "      <td>52</td>\n",
       "      <td>52</td>\n",
       "      <td>male</td>\n",
       "      <td>23.0</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ddfcc580-576c-42c7-84d9-1d2298d6f246</td>\n",
       "      <td>69</td>\n",
       "      <td>445</td>\n",
       "      <td>52</td>\n",
       "      <td>52</td>\n",
       "      <td>female</td>\n",
       "      <td>21.0</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>f16c113f-c656-496f-bb5f-51da2ca077be</td>\n",
       "      <td>95</td>\n",
       "      <td>238</td>\n",
       "      <td>51</td>\n",
       "      <td>51</td>\n",
       "      <td>female</td>\n",
       "      <td>17.0</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.981</td>\n",
       "      <td>0.019</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3b90cde8-d47a-4b29-90bf-6e6175f158ba</td>\n",
       "      <td>94</td>\n",
       "      <td>540</td>\n",
       "      <td>48</td>\n",
       "      <td>48</td>\n",
       "      <td>female</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  e8686ff7-d0c6-4ba3-96af-243f72b70292                118   \n",
       "1  36f11517-9ca2-424f-ac5d-026569b63806                117   \n",
       "2  e19d516c-c49d-40aa-aaed-2f36bc51ccbe                 41   \n",
       "3  ddfcc580-576c-42c7-84d9-1d2298d6f246                 69   \n",
       "4  f16c113f-c656-496f-bb5f-51da2ca077be                 95   \n",
       "5  3b90cde8-d47a-4b29-90bf-6e6175f158ba                 94   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 144                   88                    88   \n",
       "1                 376                   64                    64   \n",
       "2                 676                   52                    52   \n",
       "3                 445                   52                    52   \n",
       "4                 238                   51                    51   \n",
       "5                 540                   48                    48   \n",
       "\n",
       "  faceAttributes.gender  faceAttributes.age faceAttributes.glasses  \\\n",
       "0                  male                18.0              NoGlasses   \n",
       "1                female                20.0              NoGlasses   \n",
       "2                  male                23.0         ReadingGlasses   \n",
       "3                female                21.0              NoGlasses   \n",
       "4                female                17.0         ReadingGlasses   \n",
       "5                female                19.0              NoGlasses   \n",
       "\n",
       "   faceAttributes.emotion.anger  faceAttributes.emotion.contempt  \\\n",
       "0                           0.0                            0.003   \n",
       "1                           0.0                            0.001   \n",
       "2                           0.0                            0.000   \n",
       "3                           0.0                            0.000   \n",
       "4                           0.0                            0.000   \n",
       "5                           0.0                            0.000   \n",
       "\n",
       "   faceAttributes.emotion.disgust  faceAttributes.emotion.fear  \\\n",
       "0                             0.0                          0.0   \n",
       "1                             0.0                          0.0   \n",
       "2                             0.0                          0.0   \n",
       "3                             0.0                          0.0   \n",
       "4                             0.0                          0.0   \n",
       "5                             0.0                          0.0   \n",
       "\n",
       "   faceAttributes.emotion.happiness  faceAttributes.emotion.neutral  \\\n",
       "0                             0.813                           0.184   \n",
       "1                             0.456                           0.542   \n",
       "2                             1.000                           0.000   \n",
       "3                             1.000                           0.000   \n",
       "4                             0.981                           0.019   \n",
       "5                             1.000                           0.000   \n",
       "\n",
       "   faceAttributes.emotion.sadness  faceAttributes.emotion.surprise  \n",
       "0                           0.000                              0.0  \n",
       "1                           0.001                              0.0  \n",
       "2                           0.000                              0.0  \n",
       "3                           0.000                              0.0  \n",
       "4                           0.000                              0.0  \n",
       "5                           0.000                              0.0  "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df_ax = pd.json_normalize(results)\n",
    "df_ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
